package insight;

/**
 * @author huu
 * @create 2025-08-24-10:35
 * @Description: 组合模式初认识，组合模式关键点在于如何组合
 */
public class calc {
    public static void main(String[] args) {
        //通过组合模式来写一个 1+1 + 2+3
        Expression oneAndOne = new Addition(new Value(1), new Value(1));
        Expression twoAndThree = new Addition(new Value(2), new Value(3));
        Expression oneAndOneAndTwoAndThree = new Addition(oneAndOne, twoAndThree);
        //可以打断点查看这个结构，类似一颗树形结构
        System.out.println(oneAndOneAndTwoAndThree.getvalue());
    }

    //一个接口
    interface Expression{
        int getvalue();
    }
    //获得这个值
    static class Value implements Expression{
        int value;
        public Value(int value){
            this.value = value;
        }
        public int getvalue(){
            return value;
        }
    }

    //加法实现了这个结合
    static class Addition implements Expression{
        Expression left;
        Expression right;
        public Addition(Expression left, Expression right){
            this.left = left;
            this.right = right;
        }
        public int getvalue(){
            return left.getvalue() + right.getvalue();
        }
    }

}
